package com.neiquan.weiguan.zip.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.neiquan.weiguan.bean.ChannelItem;
import com.neiquan.weiguan.db.DBUtil;
import com.neiquan.weiguan.db.SQLHelper;
import com.neiquan.weiguan.zip.ChannelZip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 栏目数据操作类
 */
public class ChannelZipImpl implements ChannelZip {
    private DBUtil dbUtil;

    public ChannelZipImpl(Context context) {
        dbUtil = DBUtil.getInstance();
    }

    @Override
    public boolean addCache(ChannelItem item) {
        // TODO Auto-generated method stub
        boolean flag = false;
            long id = -1;
            ContentValues values = new ContentValues();
            values.put("name", item.getName());
            values.put("id", item.getId());
            values.put("orderId", item.getOrderId());
            values.put("selected", item.getSelected());
            id = dbUtil.insertData(SQLHelper.TABLE_CHANNEL, values);
            flag = (id != -1 ? true : false);

        return flag;
    }

    @Override
    public boolean deleteCache(String whereClause, String[] whereArgs) {
        // TODO Auto-generated method stub
        boolean flag = false;
        int count = 0;

            count = dbUtil.deleteData(SQLHelper.TABLE_CHANNEL, whereClause, whereArgs);
            flag = (count > 0 ? true : false);

        return flag;
    }

    @Override
    public boolean updateCache(ContentValues values, String whereClause,
                               String[] whereArgs) {
        // TODO Auto-generated method stub
        boolean flag = false;
        int count = 0;

            count = dbUtil.updateData(SQLHelper.TABLE_CHANNEL, values, whereClause, whereArgs);
            flag = (count > 0 ? true : false);

        return flag;
    }

    @Override
    public Map<String, String> viewCache(String selection,
                                         String[] selectionArgs) {
        // TODO Auto-generated method stub
        Cursor cursor = null;
        Map<String, String> map = new HashMap<String, String>();

            cursor = dbUtil.selectData(SQLHelper.TABLE_CHANNEL, null, selection,
                    selectionArgs, null, null, null);
            int cols_len = cursor.getColumnCount();
            while (cursor.moveToNext()) {
                for (int i = 0; i < cols_len; i++) {
                    String cols_name = cursor.getColumnName(i);
                    String cols_values = cursor.getString(cursor
                            .getColumnIndex(cols_name));
                    if (cols_values == null) {
                        cols_values = "";
                    }
                    map.put(cols_name, cols_values);
                }
            }

        return map;
    }

    @Override
    public List<Map<String, String>> listCache(String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Cursor cursor = null;

            cursor = dbUtil.selectData(SQLHelper.TABLE_CHANNEL, null, selection, selectionArgs, null, null, null);
            int cols_len = cursor.getColumnCount();
            while (cursor.moveToNext()) {
                Map<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < cols_len; i++) {

                    String cols_name = cursor.getColumnName(i);
                    String cols_values = cursor.getString(cursor
                            .getColumnIndex(cols_name));
                    if (cols_values == null) {
                        cols_values = "";
                    }
                    map.put(cols_name, cols_values);
                }
                list.add(map);
            }


        return list;
    }

    public void clearFeedTable() {
//        dbUtil.deleteData(SQLHelper.TABLE_CHANNEL, null, new String[]{});
        String sql = "DELETE FROM " + SQLHelper.TABLE_CHANNEL + ";";
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        db.execSQL(sql);
        revertSeq();
    }

    private void revertSeq() {
        String sql = "update sqlite_sequence set seq=0 where name='"
                + SQLHelper.TABLE_CHANNEL + "'";
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        db.execSQL(sql);
//        ContentValues contentValues = new ContentValues();
//        contentValues.put("seq", 0);
//        dbUtil.updateData("sqlite_sequence", contentValues, "name = ?", new String[]{SQLHelper.TABLE_CHANNEL});
    }

}
